package cn.csc.module.pay.controller.app.refund;

import cn.csc.framework.pay.core.client.PayCommonResult;
import cn.csc.framework.tenant.core.aop.TenantIgnore;
import cn.csc.module.pay.api.refund.dto.PayRefundCreateReqDTO;
import cn.csc.module.pay.api.refund.dto.PayRefundStatusReqDTO;
import cn.csc.module.pay.api.refund.dto.PayRefundStatusRespDTO;
import cn.csc.module.pay.convert.refund.PayRefundConvert;
import cn.csc.module.pay.dal.dataobject.refund.PayRefundDO;
import cn.csc.module.pay.dal.mysql.refund.PayRefundMapper;
import cn.csc.module.pay.service.refund.PayRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.math.BigInteger;


@Api(tags = "用户 APP - 退款订单")
@RestController
@RequestMapping("/pay/refund")
@Validated
@Slf4j
public class AppPayRefundController {

    @Autowired
    private PayRefundService payRefundService;

    @Resource
    private PayRefundMapper refundMapper;

    @TenantIgnore
    @PermitAll
    @PostMapping("/refundOrder")
    @ApiOperation(value = "创建退款订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", required = true, value = "应用ID", example = "code", dataTypeClass = String.class),
            @ApiImplicitParam(name = "userIp", value = "用户ip", required = true, example = "127.0.0.1", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reason", value = "退款描述", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "merchantOrderId", value = "商户订单号", required = true, example = "2023125648975", dataTypeClass = String.class),
            @ApiImplicitParam(name = "amount", value = "退款金额", required = true, example = "1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "merchantRefundNo", value = "退款订单号", example = "2023125648975", dataTypeClass = String.class), // 多个使用空格分隔
    })
    public PayCommonResult<BigInteger> refundOrder(@Validated @RequestBody PayRefundCreateReqDTO reqVO ) {
        return PayCommonResult.success(payRefundService.createPayRefund(reqVO));
    }

    @TenantIgnore
    @PermitAll
    @PostMapping("/refundStatus")
    @ApiOperation(value = "退款订单状态查询")
    public PayCommonResult<PayRefundStatusRespDTO> refundStatus(@Validated @RequestBody PayRefundStatusReqDTO reqVO ) {
        PayRefundDO payRefundDO = refundMapper.selectByAppIdAndMerchantRefundId(reqVO.getAppId(), reqVO.getMerchantRefundNo(), reqVO.getMerchantOrderId());
        return PayCommonResult.success(PayRefundConvert.INSTANCE.convert3(payRefundDO));
    }

}
